package name.tp.config;

import org.springframework.batch.core.configuration.BatchConfigurationException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.SimpleJobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.support.JobRepositoryFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * @author Created by Tp-wangwh on 2019-10-30.
 */
@Configuration
@Order(50)
public class JobConfig {
    @Autowired
    private DataSource firstDataSource ;
    @Autowired
    private PlatformTransactionManager  txManager;
    @Bean(name="jobRepository")
    public JobRepository getJobRepository()
            {
        JobRepositoryFactoryBean factoryBean = new JobRepositoryFactoryBean();
        factoryBean.setDataSource(firstDataSource);
        factoryBean.setTransactionManager(txManager);
        factoryBean.setIsolationLevelForCreate("ISOLATION_READ_COMMITTED");
        try {
            factoryBean.afterPropertiesSet();
            return factoryBean.getObject();
        } catch (Exception e) {
            throw new BatchConfigurationException(e);
        }
    }
    @Bean
    public JobLauncher createJobLauncher(@Qualifier(value = "jobRepository") JobRepository jobRepository) throws Exception {
        SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
        jobLauncher.setJobRepository(jobRepository);
        jobLauncher.afterPropertiesSet();
        return jobLauncher;
    }


}
